package com.leo.erp.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.erp.dao.SystemConfigMapper;
import com.leo.erp.dto.SystemConfigExecution;
import com.leo.erp.entity.SystemConfig;
import com.leo.erp.enums.SystemConfigStateEnum;
import com.leo.erp.service.LogService;
import com.leo.erp.service.SystemConfigService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class SystemConfigServiceImpl implements SystemConfigService{
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	@Autowired
	private LogService logService;
	
	@Override
	public SystemConfigExecution deleteSystemConfig(int systemConfigId) {
		SystemConfigExecution se = new SystemConfigExecution();
		
		try {
			int effectedNum = systemConfigMapper.deleteById(systemConfigId);
			if(effectedNum > 0) {
				logService.insertLog("系统配置", "删除id=" + systemConfigId, ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				se.setState(SystemConfigStateEnum.SUCCESS.getState());
			}else {
				se.setState(SystemConfigStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}	
		return se;
	}

	@Override
	@Transactional
	public SystemConfigExecution addSystemConfig(SystemConfig systemConfig) {
		SystemConfigExecution se = new SystemConfigExecution();
		try {
			int effectedNum = systemConfigMapper.insert(systemConfig);
			if(effectedNum > 0) {
				logService.insertLog("系统配置", "新增" + systemConfig.getCompanyName().toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				se.setState(SystemConfigStateEnum.SUCCESS.getState());
			}else {
				se.setState(SystemConfigStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return se;
	}

	@Override
	public SystemConfigExecution checkIsNameExist(String companyName) {
		SystemConfigExecution se = new SystemConfigExecution();
		QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper<>();
		if(companyName != null && companyName != "") {
			queryWrapper.eq("companyName", companyName);
		}
		
		try {
			List<SystemConfig> list = systemConfigMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				se.setState(SystemConfigStateEnum.SUCCESS.getState());
			}else {
				se.setState(SystemConfigStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return se;
	}

	@Override
	public SystemConfigExecution updateSystemConfig(SystemConfig systemConfig) {
		SystemConfigExecution se = new SystemConfigExecution();
		try {
			int effectedNum = systemConfigMapper.updateById(systemConfig);
			if(effectedNum > 0) {
				logService.insertLog("系统配置", "修改" + systemConfig.getCompanyName().toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				se.setState(SystemConfigStateEnum.SUCCESS.getState());
			}else {
				se.setState(SystemConfigStateEnum.OFFLINE.getState());
			}
			
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return se;
	}

	@Override
	public SystemConfigExecution getPageList(String companyName, int currentPage, int pageSize) {
		SystemConfigExecution se = new SystemConfigExecution();
		//查询条件
		QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper<>();
		//判断
		if(companyName != null && companyName != "") {
			queryWrapper.like("companyName", companyName);
		}
		Page<SystemConfig> page = new Page<>(currentPage, pageSize);//分页查询
		IPage<Map<String, Object>> iPage = systemConfigMapper.selectMapsPage(page, queryWrapper);
		if(iPage.getRecords().size() > 0) {
			se.setState(SystemConfigStateEnum.SUCCESS.getState());
			se.setPages(page.getPages());//总页数
			se.setTotal(iPage.getTotal());//总记录数
			
			se.setSystemConfigPageList(iPage.getRecords());
		}else{
			se.setState(SystemConfigStateEnum.OFFLINE.getState());
		}
		
		return se;
	}

	/**
	 * 获取客户开关
	 */
	@Override
	public boolean getCustomerFlag() {
		boolean customerFlag = false;
        List<SystemConfig> list = getSystemConfig();
        if(list.size()>0) {
            String flag = list.get(0).getCustomerFlag();
            if(("1").equals(flag)) {
                customerFlag = true;
            }
        }
        return customerFlag;
	}

	private List<SystemConfig> getSystemConfig() {
		QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("deleteFlag", "1");
		List<SystemConfig> list = null;
		try {
			list = systemConfigMapper.selectList(queryWrapper);
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean getDepotFlag() {
		boolean depotFlag = false;
        List<SystemConfig> list = getSystemConfig();
        if(list.size() > 0) {
            String flag = list.get(0).getDepotFlag();
            if(("1").equals(flag)) {
                depotFlag = true;
            }
        }
        return depotFlag;
	}

}
